package flaud

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
)

type EvidenceTyService struct{}

// CreateEvidenceTy 创建证据类型管理记录
// Author [yourname](https://github.com/yourname)
func (ETService *EvidenceTyService) CreateEvidenceTy(ctx context.Context, ET *flaud.EvidenceTy) (err error) {
	err = global.GVA_DB.Create(ET).Error
	return err
}

// DeleteEvidenceTy 删除证据类型管理记录
// Author [yourname](https://github.com/yourname)
func (ETService *EvidenceTyService) DeleteEvidenceTy(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&flaud.EvidenceTy{}, "id = ?", ID).Error
	return err
}

// DeleteEvidenceTyByIds 批量删除证据类型管理记录
// Author [yourname](https://github.com/yourname)
func (ETService *EvidenceTyService) DeleteEvidenceTyByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]flaud.EvidenceTy{}, "id in ?", IDs).Error
	return err
}

// UpdateEvidenceTy 更新证据类型管理记录
// Author [yourname](https://github.com/yourname)
func (ETService *EvidenceTyService) UpdateEvidenceTy(ctx context.Context, ET flaud.EvidenceTy) (err error) {
	err = global.GVA_DB.Model(&flaud.EvidenceTy{}).Where("id = ?", ET.ID).Updates(&ET).Error
	return err
}

// GetEvidenceTy 根据ID获取证据类型管理记录
// Author [yourname](https://github.com/yourname)
func (ETService *EvidenceTyService) GetEvidenceTy(ctx context.Context, ID string) (ET flaud.EvidenceTy, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&ET).Error
	return
}

// GetEvidenceTyInfoList 分页获取证据类型管理记录
// Author [yourname](https://github.com/yourname)
func (ETService *EvidenceTyService) GetEvidenceTyInfoList(ctx context.Context, info flaudReq.EvidenceTySearch) (list []flaud.EvidenceTy, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&flaud.EvidenceTy{})
	var ETs []flaud.EvidenceTy
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&ETs).Error
	return ETs, total, err
}
func (ETService *EvidenceTyService) GetEvidenceTyPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
